#make followers csv

import tweepy #https://github.com/tweepy/tweepy
import csv
import argparse
import time
 
#Twitter API credentials
consumer_key = "KU8y0jCijZYTZKD1JNXxyuCe4"
consumer_secret = "IRdkpvddpsXS9QF5AMnrdMx9CN0Mqc0O52zFu1iXTE723F1fgt"
access_key = "1346256042-7aJMv5FlYas6ue27nKxFEi8PRNExDGSAFODJd20"
access_secret = "gEplKhvd07i5eEJX51osZhLyX4KnDiFqyCUc3UQAnR0lI"
 
def get_followers_for_user(screen_name):
    #Twitter only allows access to a users most recent 3240 tweets with this method
    
    #authorize twitter, initialize tweepy
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth)

    followers = []
    n = 0
    
    for follower in tweepy.Cursor(api.followers, screen_name=screen_name).items():
        if n < 10:
            followers =  followers + [[follower.screen_name, follower.followers_count]]             
            n += 1

    return followers
    


def get_all_followers(screen_name):

    followers = []

    #authorize twitter, initialize tweepy
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth)

    
    userInfo = tweepy.Cursor(api.followers, screen_name=screen_name).items()
 
    list= open('/<a title="home" href="http://resources.infosecinstitute.com/">home</a>/hkhrais/Desktop/list.txt','w')
     
    if(api.verify_credentials):
        print 'We sucessfully logged in'
     
    user = tweepy.Cursor(api.followers, screen_name="arabbankgroup").items()
     
    while True:
        try:
            u = next(user)
            list.write(u.screen_name +' n')
     
        except:
            time.sleep(15*60)
            print 'We got a timeout ... Sleeping for 15 minutes'
            u = next(user)
            list.write(u.screen_name +' n')
    list.close()
    #n = 0
##    for follower in:
##        if n < 10:
##            followers = followers + [[follower.screen_name, follower.followers_count]]# + get_followers_for_user(follower.screen_name)
##            n += 1

    with open('%s_numfollowers.csv' % screen_name, 'w') as f_2:
        writer = csv.writer(f_2)
        writer.writerow(["user", "numFollowers"])
        writer.writerows(followers)
    

##if __name__ == '__main__':
##	#pass in the username of the account you want to download
##        parser = argparse.ArgumentParser(description='Get All Tweets From User')
##        parser.add_argument("user", metavar="USER", type=str, help="Downloads tweets for USER")   
##        arg = parser.parse_args()
##        get_all_followers(arg.user)
